package com.bizprocess;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

public class SignalEventBizTest {
	@org.junit.Test
	public void test() {
		ProcessEngine engine = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource(
						"temp/activiti.cfg.xml").buildProcessEngine();
		RepositoryService repositoryService = engine.getRepositoryService();
		Deployment deployment = repositoryService.createDeployment()
				.addClasspathResource("diagrams/SignalEventTest.bpmn")
				.addClasspathResource("diagrams/SignalEventTest2.bpmn")
				.deploy();
		TaskService taskService = engine.getTaskService();
		RuntimeService runtimeService = engine.getRuntimeService();
		String result = "true";
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("result", result);
		ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey("signalEventTest");
		ProcessInstance processInstance1 = runtimeService
				.startProcessInstanceByKey("signalEventTest");

		/**
		 * 完成每个流程的前两个用户任务
		 */
		Task task = taskService.createTaskQuery()
				.processInstanceId(processInstance.getId()).singleResult();
		taskService.complete(task.getId());
		Task task11 = taskService.createTaskQuery()
				.processInstanceId(processInstance1.getId()).singleResult();
		taskService.complete(task11.getId());
		// Task task1 =
		// taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
		// System.out.println(task1.getName());
		Task task1 = taskService.createTaskQuery()
				.processInstanceId(processInstance.getId()).singleResult();
		taskService.complete(task1.getId());
		Task task12 = taskService.createTaskQuery()
				.processInstanceId(processInstance1.getId()).singleResult();
		taskService.complete(task12.getId());
		Task task2 = taskService.createTaskQuery()
				.processInstanceId(processInstance.getId()).singleResult();
		taskService.complete(task2.getId());
		Task task13 = taskService.createTaskQuery()
				.processInstanceId(processInstance1.getId()).singleResult();
		taskService.complete(task13.getId());
		// 第一个流程的执行流
		Execution execution1 = runtimeService.createExecutionQuery()
				.processInstanceId(processInstance.getId())
				.activityId("signalintermediatecatchevent1").singleResult();
		// 第二个流程的执行流
		Execution execution2 = runtimeService.createExecutionQuery()
				.processInstanceId(processInstance1.getId())
				.activityId("signalintermediatecatchevent1").singleResult();

		// 启动下一个流程
		ProcessInstance proInstance=runtimeService.startProcessInstanceByKey("signalEventTest2");
		
		Task t = taskService.createTaskQuery()
				.processInstanceId(proInstance.getId()).singleResult();
		taskService.complete(t.getId());
		

		// runtimeService.setVariable(execution.getId(),"result", "true");
		// runtimeService.signalEventReceived("end", vars);
		// runtimeService.signalEventReceived("end", execution1.getId(), vars);
		Task task3 = taskService.createTaskQuery()
				.processInstanceId(processInstance.getId()).singleResult();
		System.out.println(task3.getName());

		Task task23 = taskService.createTaskQuery()
				.processInstanceId(processInstance1.getId()).singleResult();
		System.out.println(task23.getName());

	}
}
